package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.vo.CatalogVO;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService{

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private StrategyRepository strategyRepository;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        Destination destination = destinationRepository.findById(strategyCatalog.getDestId()).get();
        strategyCatalog.setDestName(destination.getName());
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        Destination destination = destinationRepository.findById(strategyCatalog.getDestId()).get();
        strategyCatalog.setDestName(destination.getName());
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public StrategyCatalog get(String id) {
        StrategyCatalog strategyCatalog = strategyCatalogRepository.findById(id).orElse(null);
        return strategyCatalog;
    }

    @Override
    public List<StrategyCatalog> list() {

        return strategyCatalogRepository.findAll();
    }

    @Override
    public Page<StrategyCatalog> queryPageList(QueryObject qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        return DBHelper.query(template,StrategyCatalog.class,query,pageable);
    }

    /**
     * 查询下拉攻略分类表
     * @return
     */
    @Override
    public List<CatalogVO> queryStrategyCatalogList() {
        List<CatalogVO> list = new ArrayList<>();
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults();
        for (Map data : datas) {
            CatalogVO catalogVO = new CatalogVO();
            //设置上destName字段的值
            catalogVO.setDestName(data.get("_id").toString());
            List<Object> names= (List<Object>) data.get("names");
            List<Object> ids= (List<Object>) data.get("ids");
            List<StrategyCatalog> mapList = new ArrayList<>() ;
            //设置mapList上的值
            for (int i = 0 ; i < names.size();i++){
                StrategyCatalog strategyCatalog = new StrategyCatalog();
                /*strategyCatalog.setName(names.get(i).toString());
                strategyCatalog.setId(ids.get(i).toString());*/
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                strategyCatalog.setName(name);
                strategyCatalog.setId(id);
                mapList.add(strategyCatalog);
            }
            catalogVO.setMapList(mapList);
            list.add(catalogVO);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> getCatalogs(String destId) {
        List<StrategyCatalog> catalogs = strategyCatalogRepository.findByDestId(destId);
        for (StrategyCatalog catalog : catalogs) {
            /*List<Strategy> strategies = new ArrayList<>()*/;
            List<Strategy> strategies = strategyRepository.findByCatalogId(catalog.getId());
            catalog.setStrategies(strategies);
        }
        return catalogs;
    }


}
